define(["jquery","bbcare","fu/app/comm/page/page",
    "scprit/nutrition/app/nutritionApplication/nutritionApplicationList.ui.js",
    "scprit/nutrition/app/nutritionApplication/nutritionApplicationList.servers.js"],
    function($,$bb,$page,$ui,$servers){
    var event = {};

    /**
     * 跳转到添加营养方案页面
     */
    event.onAddNutritionScheme = function(){
        $bb.openPage("addNutritionScheme");
    }

    /**
     * 根据姓名和手机号码搜索
     */
    var onKeywordInput = function(){
        var params = $(this).data();
    	var searchCondition = $(this).val();
    	params = $.extend(params,{"searchCondition":searchCondition,"page":1});
    	$servers.ajaxList(params,ajaxListCallBack);
    }

    /**
     * 点击某个患者进入详情页
     */
    var onMonitorUserCardSelect = function(){
        var ptSchemeId = $(this).attr("ptSchemeId");
        var userId = $(this).attr("userId");
        var phoneNo = $(this).attr("phone");
        var params = {"ptSchemeId":ptSchemeId,"userId":userId,"phoneNo":phoneNo};
    	$bb.openPage("nutritionApplicationDetails",{"value":params});
    }

    /**
     * 请求患者营养方案列表成功后的回调函数
     */
    var ajaxListCallBack = function(data,params){
    	if(data.rows.length >= 0){
            var pagesAll = Math.ceil(data.total / params.rows);
    		$ui.listInit({
	        	"id":"monitorUserBox",
	        	"list":data.rows,
                "rows":params.rows
	        });
            /* 翻页 */
            $page.turnInit({
                id:"monitorUserPage",
                pagesAll:pagesAll,
                page:params.page
            },$servers.ajaxList,params,ajaxListCallBack);
	        $("#monitorUserBox .monitor-user-card.click-able").click(onMonitorUserCardSelect);
    	}
        $("#key").unbind("input propertychange");
        $("#key").data(params);
        $("#key").bind("input propertychange",onKeywordInput);
        $("#search").unbind("click");
        $("#search").click(function(){
            getListAll(1);
        });
        $("#getAll button").unbind("click");
        $("#getAll button").click(function(){
            getListAll(1);
        });
    };

    /**
     * 默认 全部患者记录
     */
    var getListAll = function(page){
        $("#key").val("");
    	var params = {
            "rows":12,
            "page":page,
            "searchCondition":""
        };
    	$servers.ajaxList(params,ajaxListCallBack);
    }
    event.getListAll = function(page){
        getListAll(page);
    };

    return event;
});